﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace XNADota
{
    public class FrameRate : XNADotaObject
    {
        float fps;
        public float updateInterval = 1.0f;
        float timeSinceLastUpdate = 0.0f;
        float frameCount = 0;

        string Write = "";

        private bool _DisplayFrameRate = false;

        public bool DisplayFrameRate
        {
            set { _DisplayFrameRate = value; }
            get { return _DisplayFrameRate; }
        }

        public FrameRate(Game game)
            : base(game)
        {
            Reset();
        }

        public void Reset()
        {
            fps = 0;
            updateInterval = 1.0f;
            timeSinceLastUpdate = 0.0f;
            frameCount = 0;
        }

        public void Update(GameTime gameTime)
        {
            float elapsed = (float)gameTime.ElapsedRealTime.TotalSeconds;
            frameCount++;
            timeSinceLastUpdate += elapsed;

            if (timeSinceLastUpdate > updateInterval)
            {
                fps = frameCount / timeSinceLastUpdate;

                Write = "FPS: " + fps.ToString() + " - " +
                            gameTime.ElapsedRealTime.TotalSeconds.ToString(".###");

                frameCount = 0;
                timeSinceLastUpdate -= updateInterval;
            }

            if (DisplayFrameRate)
            {
                System.Diagnostics.Debug.WriteLine(Write);
            }
        }
    }
}
